import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null,
    token: null,
    isLogin: false,
    themeMode: 'light', // light, dark
    _initialized: false // 标记是否已初始化
  }),
  
  getters: {
    // 获取用户头像
    avatar: (state) => {
      return state.userInfo?.avatar || '/static/default-avatar.png'
    },
    
    // 获取用户名
    username: (state) => {
      return state.userInfo?.username || '未登录'
    },
    
    // 获取用户ID
    userId: (state) => {
      return state.userInfo?.id || null
    }
  },
  
  actions: {
    // 设置用户信息
    setUserInfo(info) {
      this.userInfo = info
      this.isLogin = true
      // 持久化存储
      uni.setStorageSync('userInfo', info)
    },
    
    // 设置token
    setToken(token) {
      this.token = token
      uni.setStorageSync('token', token)
    },
    
    // 设置刷新token
    setRefreshToken(refreshToken) {
      uni.setStorageSync('refreshToken', refreshToken)
    },
    
    // 登录
    login(userInfo, token, refreshToken) {
      this.setUserInfo(userInfo)
      this.setToken(token)
      if (refreshToken) {
        this.setRefreshToken(refreshToken)
      }
    },
    
    // 退出登录
    logout() {
      this.userInfo = null
      this.token = null
      this.isLogin = false
      
      // 清除本地存储
      uni.removeStorageSync('token')
      uni.removeStorageSync('refreshToken')
      uni.removeStorageSync('userInfo')
      
      // 跳转到登录页
      uni.reLaunch({
        url: '/pages/login/login'
      })
    },
    
    // 更新用户信息
    updateUserInfo(info) {
      this.userInfo = { ...this.userInfo, ...info }
      uni.setStorageSync('userInfo', this.userInfo)
    },
    
    // 设置主题模式
    setThemeMode(mode) {
      this.themeMode = mode
      uni.setStorageSync('themeMode', mode)
      
      // #ifdef H5
      // H5平台更新data-theme属性
      if (typeof document !== 'undefined') {
        document.documentElement.setAttribute('data-theme', mode)
      }
      // #endif
    },
    
    // 初始化用户状态
    initUserState() {
      // 防止重复初始化
      if (this._initialized) return
      
      const token = uni.getStorageSync('token')
      const userInfo = uni.getStorageSync('userInfo')
      const themeMode = uni.getStorageSync('themeMode') || 'light'
      
      if (token && userInfo) {
        this.token = token
        this.userInfo = userInfo
        this.isLogin = true
      }
      
      // 确保themeMode始终有值
      this.themeMode = themeMode || 'light'
      
      // #ifdef H5
      // H5平台更新data-theme属性
      if (typeof document !== 'undefined') {
        document.documentElement.setAttribute('data-theme', this.themeMode)
      }
      // #endif
      
      // 标记已初始化
      this._initialized = true
    },
    
    // 检查登录状态
    checkLoginStatus() {
      if (!this.isLogin || !this.token) {
        uni.showModal({
          title: '提示',
          content: '请先登录',
          showCancel: false,
          success: () => {
            uni.navigateTo({
              url: '/pages/login/login'
            })
          }
        })
        return false
      }
      return true
    },
    
    // 处理token失效
    handleTokenExpired() {
      this.userInfo = null
      this.token = null
      this.isLogin = false
      
      // 清除本地存储
      uni.removeStorageSync('token')
      uni.removeStorageSync('refreshToken')
      uni.removeStorageSync('userInfo')
    }
  }
}) 